package com.kehutong.payfees.admin;

import com.kehutong.common.DeptService;
import com.kehutong.common.PageUtils;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import com.kehutong.payfees.entity.ChargeBill;
import com.kehutong.payfees.entity.receipt.ReceiptOrder;
import com.kehutong.payfees.entity.receipt.ReceiptStatistics;
import com.kehutong.payfees.service.receipt.ReceiptOrderServiceImpl;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import java.util.List;

/**
 * @program: kht
 * @description:
 * @author: joneron
 * @create: 2021/6/2
 **/
@MvcAction
@ReqMapping("/admin/crm/charge/receipt/order")
public class ReceiptOrderController {

    private static final Logger logger = LoggerFactory.getLogger(ReceiptOrderController.class);

    @Inject
    ReceiptOrderServiceImpl receiptOrderService;

    @Inject
    DeptService deptService;

    @ReqMapping("/page")
    @HasPermission("crm:charge:receipt:order:view")
    public Object page(JSONObject jsonObject) {
        return receiptOrderService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("crm:charge:receipt:order:view")
    public Object list(JSONObject jsonObject) {
        return receiptOrderService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/save")
    public Object save(ReceiptOrder entity, Token token) throws Exception {

        List<String> departList = token.getDepartments();
        if(null == departList || departList.size() == 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());
        }

        DeptDTO deptDTO = deptService.getDept(departList.get(0));
        entity.setDepartmentNo(deptDTO.getDepartmentNo());
        entity.setDepartmentNos(deptDTO.getDepartmentNos());
        entity.setDepartmentName(deptDTO.getDepartmentName());

        return receiptOrderService.save(entity);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/manualSave")
    public Object manualSave(ChargeBill chargeBill, Token token) throws Exception {

        return receiptOrderService.savePdf(chargeBill,token.getCompanyNo());
    }

    @Bean(copy=true)
    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/update")
    public Object update(ReceiptOrder entity) {

        return receiptOrderService.update(entity);
    }

    @HasPermission("crm:charge:receipt:order:edit")
    @ReqMapping("/delete")
    public Object delete(ReceiptOrder entity) {

        return receiptOrderService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(ReceiptOrder entity) {

        return Result.success(entity);
    }

    /**
     * 统计功能
     */
    @ReqMapping("/receiptStat")
    @HasPermission("crm:charge:receipt:order:view")
    public Object receiptStat(JSONObject jsonObject,Token token) throws Exception {

        List<ReceiptStatistics> listObj = receiptOrderService.groupData(jsonObject,token);

        return PageUtils.buildPage(jsonObject,listObj);
    }



}
